package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.InformDemo;
import com.mrd.gtimp.base.service.InformDemoBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */

@Controller
@RequestMapping("/base")
public class InformDemoAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private InformDemoBiz informDemoBiz;

    public List<InformDemo> demos;

    public InformDemo demo;

    private String title;

    private String left;

    private String right;

    @RequestMapping("/demo-main")
    public String main(Model model) {
//        title = "业务通知模版管理";
//        left = "demo-tree.do";
//        right = "";
        model.addAttribute("title", "业务通知模版管理");
        model.addAttribute("left", "demo-tree");
        model.addAttribute("right", "");
        return WebPathConstants.FRAME_WEB_PATH;
    }

    @RequestMapping("/demo-tree")
    public String tree() {
        return "/gtimp/base/inform/informDemo";
    }

    @RequestMapping("/demo-save")
    @ResponseBody
    public Result save() {
        Result msg;
        try {
            if (demo != null) {
                if (StringUtils.isEmpty(demo.getId())) {
                    demo.setId(null);
                    demo.setCtime(new Date());
                    demo.setCreator("");
                } else {
                    demo.setModifier("");
                    demo.setMtime(new Date());
                }
                informDemoBiz.saveOrupdateEntity(demo);
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (SaveException e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    @RequestMapping("/demo-find")
    public String find(Jqgrid jqgrid) {
        try {
            String typeId = demo.getTypeId();
            String nodeId = demo.getNodeId();
            if (StringUtils.isNotEmpty(typeId) && StringUtils.isNotEmpty(nodeId)) {
//                if (filters == null) {
//                    filters = new Filter();
//                    filters.setGroupOp(FilterGroupOption.AND);
//                }
                jqgrid.addRule(new SearchRule("typeId", SearchRuleOption.eq, typeId));
                jqgrid.addRule(new SearchRule("nodeId", SearchRuleOption.eq, nodeId));
//                List<InformDemo> list = informDemoBiz.findByJqgrid(0, 0, filters, null);
                Page<InformDemo> list = informDemoBiz.findByJqgrid(jqgrid);
                if (list != null && list.getSize() == 1) {
                    demo = list.getContent().get(0);
                }
            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/base/inform/editDemo";
    }

    @RequestMapping("/demo-listDemo")
    @ResponseBody
    public Page<InformDemo> listDemo(Jqgrid jqgrid) {
        try {
//            page = initPage(informDemoBiz.findEntityCount(filters));
//            demos = informDemoBiz.findByJqgrid(page.getCurrentPageStartNo(), page
//                    .getPageSize(), filters, null);
            Page<InformDemo> demos=informDemoBiz.findByJqgrid(jqgrid);
            if(demos.getSize()>0){
                return  demos;
            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }

    /* set get method */
    public List<InformDemo> getDemos() {
        return demos;
    }

    public void setDemos(List<InformDemo> demos) {
        this.demos = demos;
    }

    public InformDemo getDemo() {
        return demo;
    }

    public void setDemo(InformDemo demo) {
        this.demo = demo;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLeft() {
        return left;
    }

    public void setLeft(String left) {
        this.left = left;
    }

    public String getRight() {
        return right;
    }

    public void setRight(String right) {
        this.right = right;
    }

}
